package com.mdd.common.mapper.order;

import com.mdd.common.core.basics.IBaseMapper;
import com.mdd.common.entity.order.Order;
import com.mdd.common.vo.OrderVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 订单Mapper
 * @author shenjian
 * @createTime 2023年02月11日 15:06:00
 */
@Mapper
public interface OrderMapper extends IBaseMapper<Order> {

    @Select("<script>" +
            "SELECT o.id, u.sn as userId, o.order_no as orderNo, o.`status`, m.id as mansionId, m.`name` as mansionName, a.floor, o.meal_number mealNumber, o.total_amount as totalAmountF," +
            " o.coupon_amount as couponAmountF, o.actual_amount as actualAmountF, o.delivery_time as deliveryTime, o.create_time as createTime, o.pay_time as payTime\n" +
            "FROM ${prefix}order o\n" +
            "LEFT JOIN ${prefix}user u on u.id = o.user_id\n" +
            "LEFT JOIN ${prefix}order_detail od ON od.order_id = o.id\n" +
            "LEFT JOIN ${prefix}order_menu om ON om.id = od.menu_id\n" +
            "LEFT JOIN ${prefix}address a ON a.id = o.address_id\n" +
            "LEFT JOIN ${prefix}mansion m ON m.id = a.mansion_id\n" +
            "WHERE o.`status` <![CDATA[>]]> 0 \n" +
            " <if test=\" userId != null \"> AND u.sn = #{userId} </if>" +
            " <if test=\" phone != null and phone != ''\"> AND a.mobile like concat('%',#{phone},'%') </if>" +
            " <if test=\" mansionId != null \"> AND m.id = #{mansionId} </if>" +
            " <if test=\" orderNo != null and  orderNo != ''\"> AND o.order_no = #{orderNo} </if>" +
            " <if test=\" deliveryStartTime != null and deliveryEndTime != null\"> " +
            "   AND o.delivery_time <![CDATA[>=]]> #{deliveryStartTime} \n" +
            "   AND o.delivery_time <![CDATA[<=]]> #{deliveryEndTime}" +
            "</if>" +
            " <if test=\" orderTime != null and orderTime != ''\"> " +
            "   AND om.day = #{orderTime} \n" +
            "</if>" +
            "  GROUP BY o.id \n" +
            "  order by o.create_time desc" +
            "</script>")
    List<OrderVo> selectOrderListByWhere(@Param("userId") Integer userId,
                                         @Param("mansionId") Integer mansionId,
                                         @Param("orderNo") String orderNo,
                                         @Param("phone") String phone,
                                         @Param("deliveryStartTime") Long deliveryStartTime,
                                         @Param("deliveryEndTime") Long deliveryEndTime,
                                         @Param("orderTime") String orderTime);
}
